﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;


namespace BUS
{
    public class ManagerFileBUS
    {
        public static List<ManagerFileDTO> GetFileByID(String ID)
        {

            return ManagerFileDAO.GetFileByID(ID);

        }
        public static bool AddFile(ManagerFileDTO file)
        {
            return ManagerFileDAO.SaveFile(file);

        }
        public static int GetCount()
        {
            List<ManagerFileDTO> list = ManagerFileDAO.GetAllFile();
            return list.Count;
        }
        public static void AddMyPath(string MyPath)
        {
            MySqlConnection.myPath = MyPath;
        }
        public static void Export2XML(string fileName, string myPath)
        {
            DataSet ds = new DataSet();
            ds = ManagerFileDAO.GetDataSet2XML2();
            fileName += ".xml";
            fileName = myPath + fileName;
            if (File.Exists(fileName) == true)
                File.Delete(fileName);
            //create file
            //FileStream fileStream = File.Create(fileName);
            //fileStream.Close();


            //StreamWriter xmlDoc = 
            //Stream xmlDoc=
            //BinaryWriter xmlDocBinary=new BinaryWriter(File.Open(fileName, FileMode.Open));
            //FileStream file=new FileStream(fileName)
            //ds.WriteXml(xmlDoc);

            List<ManagerFileDTO> listData = new List<ManagerFileDTO>();
            listData = ManagerFileDAO.GetAllFile();
            TextWriter textWrite = new StreamWriter(fileName, true);

            XmlSerializer xmlserializer = new XmlSerializer(listData.GetType());
            xmlserializer.Serialize(textWrite, listData);

            textWrite.Close();
        }
        public static bool ImprotFromXML(string pathXML)
        {

            if (File.Exists(pathXML))
            {
                //DataSet ds = new DataSet();
                //ds.ReadXml(pathXML);


                //foreach (DataRow dr in ds.Tables[0].Rows)
                //{

                //    ManagerFileDTO fileDTO = new ManagerFileDTO();
                //    fileDTO.Id = dr["ID"].ToString();
                //    fileDTO.Extension = dr["Extension"].ToString();
                //    fileDTO.Name = dr["Name"].ToString();
                //    fileDTO.Size = float.Parse(dr["Size"].ToString());

                //    //System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
                //    //System.Text.UTF8Encoding encoding = new UTF8Encoding();

                //    //fileDTO.Data = encoding.GetBytes(dr["data"].ToString());

                //    //System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                //    //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[]));
                //    //serializer.Serialize(memStream, dr["Data"]);

                //    ////fileDTO.Data = memStream.ToArray();
                //    //BinaryFormatter binaryFormatter = new BinaryFormatter();
                //    //binaryFormatter.Deserialize()
                //    //fileDTO.Data = (byte[])(dr["Data"]);

                //ManagerFileBUS.AddFile(fileDTO);
                List<ManagerFileDTO> listFileDTO = new List<ManagerFileDTO>();

                TextReader textReader = new StreamReader(pathXML);
                XmlSerializer xmlDeserializer = new XmlSerializer(listFileDTO.GetType());
                listFileDTO = xmlDeserializer.Deserialize(textReader) as List<ManagerFileDTO>;
                foreach (ManagerFileDTO manager in listFileDTO)
                {
                    ManagerFileBUS.AddFile(manager);
                }
                
                textReader.Close();
                return true;



            }
            else

                return false;
            //ds.ReadXml();




        }


        public static List<ManagerFileDTO> GetAllFile()
        {
            return ManagerFileDAO.GetAllFile();
        }
    }
}
